//
//  DetailViewController.m
//  LvXingPai
//
//  Created by mafang on 16/8/12.
//  Copyright © 2016年 _Junxee. All rights reserved.
//

#import "DetailViewController.h"

// 各种cell
#import "DetailModel.h"
#import "OneDetailCell.h"
#import "TwoDetailCell.h"
#import "BuyCell.h"
#import "ReserveCell.h"
#import "TrafficCell.h"
#import "CommentCell.h"
#import "StoreCell.h"

@interface DetailViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,strong) NSMutableArray *dataArray;

@property (weak, nonatomic) IBOutlet UITableView *tableView;

@end

@implementation DetailViewController

#pragma mark - 懒加载
- (NSMutableArray *)dataArray
{
    if (_dataArray == nil) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title = @"商品详情";
    
    self.navigationController.navigationBarHidden = NO;
    
    [self setBarButStyle];
    
    // 获取网络数据
    [self downloadData];
    
    // 使用XIB注册cell
    [self.tableView registerNib:[UINib nibWithNibName:@"OneDetailCell" bundle:nil] forCellReuseIdentifier:@"OneDetailCell"];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"TwoDetailCell" bundle:nil] forCellReuseIdentifier:@"TwoDetailCell"];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"BuyCell" bundle:nil] forCellReuseIdentifier:@"BuyCell"];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"ReserveCell" bundle:nil] forCellReuseIdentifier:@"ReserveCell"];
}

#pragma mark - UITableViewDataSource
// 设置没有请求数据之前不显示cell
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (self.dataArray.count == 0) {
        return 0;
    } else {
        return 8;
    }
}

// 设置每行不同的cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        OneDetailCell *oneCell = [tableView dequeueReusableCellWithIdentifier:@"OneDetailCell"];
        oneCell.detailModel = self.dataArray[0];
        return oneCell;
        
    } else if (indexPath.row == 1) {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
        return cell;
        
    } else if (indexPath.row == 2) {
        TwoDetailCell *twoCell = [tableView dequeueReusableCellWithIdentifier:@"TwoDetailCell"];
        twoCell.detailModel = self.dataArray[0];
        return twoCell;
        
    } else if (indexPath.row == 3) {
        BuyCell *buyCell = [tableView dequeueReusableCellWithIdentifier:@"BuyCell"];
        buyCell.detailModel = self.dataArray[0];
        return buyCell;
        
    } else if (indexPath.row == 4) {
        ReserveCell *reserveCell = [tableView dequeueReusableCellWithIdentifier:@"ReserveCell"];
        reserveCell.detailModel = self.dataArray[0];
        return reserveCell;
        
    } else if (indexPath.row == 5) {
        TrafficCell *trafficCell = [tableView dequeueReusableCellWithIdentifier:@"TrafficCell"];
        trafficCell.detailModel = self.dataArray[0];
        return trafficCell;
        
    } else if (indexPath.row == 6) {
        CommentCell *commentCell = [tableView dequeueReusableCellWithIdentifier:@"CommentCell"];
        commentCell.detailModel = self.dataArray[0];
        return commentCell;
        
    } else {
        StoreCell *storeCell = [tableView dequeueReusableCellWithIdentifier:@"StoreCell"];
        storeCell.detailModel = self.dataArray[0];
        return storeCell;
    }
}

// 设置不同的行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        return 255;
    } else if (indexPath.row == 1) {
        return 35;
    } else if (indexPath.row == 2) {
        return 265;
    } else if (indexPath.row == 3) {
        return 300;
    } else if (indexPath.row == 4) {
        return 545;
    } else if (indexPath.row == 5) {
        return 155;
    } else if (indexPath.row == 6) {
        return 90;
    } else {
        return 95;
    }
}

#pragma mark - 设置导航栏的按钮样式
-(void)setBarButStyle{
    UIBarButtonItem *leftBtn = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"common_icon_navigation_back_normal"] style:UIBarButtonItemStylePlain target:self action:@selector(backAction:)];
    
    self.navigationItem.leftBarButtonItem = leftBtn;
}

#pragma mark - 详情界面数据请求
- (void)downloadData
{
    __weak typeof(self) weakSelf = self;
    
    // 配置url地址
    NSString *urlStr = [NSString stringWithFormat:@"http://api.lvxingpai.com/app/marketplace/commodities/%@",self.commodityId];
    NSURL *url = [NSURL URLWithString:urlStr];
    
    NSURLSession *session = [NSURLSession sharedSession];
    NSURLSessionDataTask *task = [session dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        
        if (error) {
            NSLog(@"详情界面请求数据失败,error==%@",error);
        } else {
            NSLog(@"详情界面数据请求成功");
            NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
            NSDictionary *d = dict[@"result"];
            DetailModel *model = [[DetailModel alloc]initWithDict:d];
            [self.dataArray addObject:model];
            
            // 回到主线程刷新表格
            dispatch_async(dispatch_get_main_queue(), ^{
                [weakSelf.tableView reloadData];
            });
        }
    }];
    [task resume];
}

#pragma mark - 返回到上一个界面
-(void)backAction:(UIBarButtonItem *)sender{
    [self.navigationController popViewControllerAnimated:YES];
}




/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
